/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import bean.PatientBean;
import org.dom4j.DocumentException;
import java.io.UnsupportedEncodingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 *
 * @author wudihua
 */
public class XmlToBeanUtil {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(XmlToBeanUtil.class);

    public static void main(String[] args) {
        String s = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Patient xmlns=\"http://hl7.org/fhir\">    <id value=\"001\"/>    <!--患者身份证号-->    <identifier>        <system value=\"2.16.840.1.113883.2.23.1.9.1\"/>        <value value=\"110101200301120019\"/>    </identifier>    <!--患者社保卡号-->    <identifier>        <system value=\"2.16.840.1.113883.2.23.1.9.2\"/>        <value value=\"100000000000\"/>    </identifier>    <!--姓名-->    <name>        <text value=\"刘康\"/>        <!--可以只提供text字段而不提供family和given-->    </name>    <!--联系电话-->    <telecom>        <system value=\"phone\"/>        <value value=\"13800138000\"/>        <use value=\"mobile\"/>    </telecom>    <!--性别-->    <gender value=\"male\"/>    <!--出生日期，时间-->    <birthDate value=\"2003-01-12\">        <extension url=\"http://hl7.org/fhir/StructureDefinition/patient-birthTime\">            <valueDateTime value=\"2003-01-12T09:12:35+08:00\"/>        </extension>    </birthDate>    <!--家庭住址-->    <address>        <use value=\"home\"/>        <text value=\"北京市东城区景山前街4号\"/>        <line value=\"景ft前街4号\"/>        <city value=\"北京市\"/>        <district value=\"东城区\"/>        <state value=\"北京\"/>        <postalCode value=\"100010\"/>    </address>    <!--联系人信息-->    <contact>        <relationship>            <coding>                <system value=\"http://hl7.org/fhir/patient-contact-relationship\"/>                <code value=\"parent\"/>            </coding>        </relationship>        <name>            <text value=\"刘勇\"/>        </name>        <telecom>            <system value=\"phone\"/>            <value value=\"13012345678\"/>        </telecom>        <gender value=\"male\"/>    </contact></Patient>\n";
        PatientBean patientBean = parsePatientXml(s);

    }

    /**
     * 解析patientXml
     *
     * @param xmlString
     * @return
     */
    public static PatientBean parsePatientXml(String xmlString) {
        LOGGER.info("patient xml : {}", xmlString);

        PatientBean patientBean = new PatientBean();


        final XmlLoadUtil xmlLoader = new XmlLoadUtil();
        xmlLoader.setBeans(patientBean);

        try {
            xmlLoader.loadXmlString(xmlString);
            String id = patientBean.getPatientId();
            String birthTime = patientBean.getBirthTime().split("T|\\+")[1];
            patientBean.setBirthTime(birthTime);
            System.out.println("id = " + id);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return patientBean;
    }  
}
